package org.example.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.bean.Constants;
import org.example.bean.UserBean;
import org.example.service.AuthService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/common")
public class LoginController {


    @Resource
    private AuthService authService;

    @PostMapping("/login")
    public UserBean login(UserBean loginUser, HttpServletRequest request){
        UserBean userBean = authService.userLogin(loginUser);
        if (null != userBean){
            log.info("登录成功");
            request.getSession().setAttribute(Constants.FLAG_CURRENT_USER,userBean);
        }
        log.info("登录失败");
        return userBean;
    }

    @PostMapping("/getCurrentUser")
    public Object getCurrentUser(HttpSession session){
        return session.getAttribute(Constants.FLAG_CURRENT_USER);
    }

    @PostMapping("/logout")
    public void logout(HttpSession session){

        session.removeAttribute(Constants.FLAG_CURRENT_USER);
    }

}
